package com.wmeimob.fastboot.jiubei.controller;

import com.github.pagehelper.PageInfo;
import com.wmeimob.fastboot.core.context.PageContext;
import com.wmeimob.fastboot.core.exception.CustomException;
import com.wmeimob.fastboot.jiubei.entity.ShopGoods;
import com.wmeimob.fastboot.jiubei.result.RestResponse;
import com.wmeimob.fastboot.jiubei.result.RestResults;
import com.wmeimob.fastboot.jiubei.service.ShopGoodsService;
import com.wmeimob.fastboot.jiubei.dto.SetUpShopGoodsDTO;
import com.wmeimob.fastboot.starter.common.entity.RichText;
import com.wmeimob.fastboot.starter.common.service.RichTextService;
import com.wmeimob.fastboot.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author wjt
 * @version 1.0
 * @className ShopGoodsController
 * @description 【商品】控制器
 * @date Fri Aug 28 00:08:28 CST 2020
 **/
@RestController
@RequestMapping("shopGoods")
@Slf4j
public class ShopGoodsController {

    @Resource
    private ShopGoodsService shopGoodsService;
    @Resource(name = "richTextServiceImpl")
    private RichTextService richTextService;

    /**
     * 商品分页查询
     *
     * @param shopGoods
     * @return
     */
    @GetMapping
    public PageInfo queryForShopGoods(ShopGoods shopGoods) {
        PageContext.startPage();
        return new PageInfo<ShopGoods>(shopGoodsService.list(shopGoods));
    }

    /**
     * 商品导出
     *
     * @param shopGoods
     * @return
     */
    @PostMapping("/export")
    public List<ShopGoods> queryForShopGoodsExport(@RequestBody ShopGoods shopGoods) {
        return shopGoodsService.list(shopGoods);
    }

    /**
     * 商品查询-<通过id查询>
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ShopGoods queryForShopGoodsById(@PathVariable("id") Object id) {
        return shopGoodsService.queryShopGoodsById(id);
    }

    /**
     * 商品添加
     *
     * @param shopGoods
     * @return
     */
    @PostMapping
    public void insertForShopGoods(@RequestBody ShopGoods shopGoods) {
        if (StringUtils.isEmpty(shopGoods.getRichId()) && !StringUtils.isEmpty(shopGoods.getRichTextValue())) {
            //表示是新增的
            addRichText(shopGoods);
        }
        shopGoodsService.addShopGoods(shopGoods);
    }

    /**
     * 商品修改
     *
     * @param shopGoods
     * @return
     */
    @PutMapping
    public void updateForShopGoods(@RequestBody ShopGoods shopGoods) {
        if (StringUtils.isEmpty(shopGoods.getRichId()) && !StringUtils.isEmpty(shopGoods.getRichTextValue())) {
            //表示是新增的
            addRichText(shopGoods);
        }
        if (!StringUtils.isEmpty(shopGoods.getRichId()) && !StringUtils.isEmpty(shopGoods.getRichTextValue())) {
            //表示修改
            editRichText(shopGoods);
        }
        shopGoodsService.modifyShopGoods(shopGoods);
    }

    /**
     * 商品删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public void removeForShopGoods(@PathVariable("id") Object id) {
        shopGoodsService.removeShopGoods(id);
    }


    private void editRichText(ShopGoods shopGoods) {
        RichText richText = new RichText();
        richText.setContent(shopGoods.getRichTextValue());
        richText.setId(shopGoods.getRichId());
        richTextService.update(richText);
    }

    /**
     * 新增富文本编辑
     */
    private void addRichText(ShopGoods shopGoods) {
        RichText richText = new RichText();
        richText.setContent(shopGoods.getRichTextValue());
        richText.setGmtCreate(new Date());
        richText.setDataId(0);
        richTextService.add(richText);
        if (StringUtils.isEmpty(richText.getId())) {
            throw new CustomException("未获取到规则富文本id~！");
        }
        shopGoods.setRichId(richText.getId());
    }

    /**
     * 设置商品积分比例
     */
    @PutMapping("/setUpProportion")
    public RestResults setUpProportion(SetUpShopGoodsDTO setUpShopGoods){
        shopGoodsService.setUpProportion(setUpShopGoods);
        return RestResponse.makeOKRsp();
    }
}
